package com.dysm.spring.security.resource.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandle {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(TokenValidateException.class)
    public ResponseEntity<Map<String, String>> tokenValidateException(HttpServletRequest request, TokenValidateException e) {
        LOG.warn("request:[{}],请求异常：{}", request.getRequestURI(), e.getMessage());
        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("code", "10006");
        returnMap.put("msg", e.getMessage());
        return ResponseEntity.ok(returnMap);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, String>> undefinedException(HttpServletRequest request, Exception e) {
        LOG.error("request:[{}],请求异常：", request.getRequestURI(), e);
        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("code", "99999");
        returnMap.put("msg", "服务器内部错误");
        return ResponseEntity.ok(returnMap);
    }
}
